package com.situation.controller;

import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.TbDepartment;
import com.situation.pojo.VO.TbDepartmentRenShuVO;
import com.situation.pojo.VO.UserDeanVO;
import com.situation.service.TbDepartmentService;
import com.situation.util.ExcelUtil;
import com.situation.util.RespUtil;
import com.situation.util.StatusEnum;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TaoMinghui
 * @create 2021-12-07 9:19
 */
@RestController
@RequestMapping("/api/department")
public class TbDepartController {

    @Autowired
    private TbDepartmentService tbDepartmentService;

    /**
     * 分页查询所有部门  （带部门人数）
     * @return
     */
    @GetMapping("/findAll")
    public RespUtil findAll(int pageNum,int pageSize){
        PageInfo<TbDepartmentRenShuVO> tbDepartmentRenShuVOPageInfo = tbDepartmentService.queryAllTbDepartmentFenYeRenShu(pageNum, pageSize);
        return RespUtil.ok().setData(tbDepartmentRenShuVOPageInfo);
    }


//    /**
//     * 查询所有部门
//     * @return
//     */
//    @GetMapping("/findAllNotRenShu")
//    public RespUtil findAllNotRenShu(){
//        List<TbDepartment> tbDepartmentList = tbDepartmentService.queryAllDepartment();
//        return RespUtil.ok().setData(tbDepartmentList);
//    }

    /**
     * 添加部门
     * @param tbDepartment
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加部门失败", operation = "添加部门")
    @PostMapping("/add")
    @RequiresPermissions({"department:add"})
    public RespUtil add(TbDepartment tbDepartment){
        int i = tbDepartmentService.addTbDepartment(tbDepartment);
        if(i == 0){
            return RespUtil.fail();
        }else {
            return RespUtil.ok();
        }
    }

//    /**
//     * 通过部门名称查找id
//     * @param name
//     * @return
//     */
//    @GetMapping("/findIdByName")
//    public RespUtil findIdByName(String name){
//        long id = tbDepartmentService.findIdByName(name);
//        if(id == 0){
//            return RespUtil.fail();
//        }else {
//            return RespUtil.ok().setData(id);
//        }
//    }

    /**
     * 通过部门id删除部门
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @DeleteMapping("delete/{id}")
    @RequiresPermissions({"department:delete"})
    public RespUtil delete(@PathVariable long id){
        int i = tbDepartmentService.deleteTbDepartmentById(id);
        if(i == 0){
            return RespUtil.fail();
        }
        return RespUtil.ok();
    }

    /**
     * 编辑部门
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"department:edit"})
    public RespUtil edit(@PathVariable long id){
        TbDepartment departmentById = tbDepartmentService.findTbDepartmentById(id);
        return RespUtil.ok().setData(departmentById);
    }

    /**
     * 更新部门
     * @param tbDepartment
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新部门失败", operation = "更新部门")
    @PutMapping("update/{id}")
    @RequiresPermissions({"department:update"})
    public RespUtil update(@PathVariable Long id, TbDepartment tbDepartment) {
        tbDepartment.setId(id);
        //设置编辑时间为当前时间
        tbDepartment.setModifiedTime(LocalDateTime.now());

        int i = tbDepartmentService.editTbDepartment(tbDepartment);
        if(i == 0){
            return RespUtil.fail();
        }
        return RespUtil.ok();
    }

    /**
     * 通过模糊部门名称查询部门列表
     * @param pageNum
     * @param pageSize
     * @param tbDepartment
     * @return
     */
    @GetMapping("/findDepartmentList")
    public RespUtil findDepartmentList(int pageNum,int pageSize,TbDepartment tbDepartment){
        PageInfo<TbDepartmentRenShuVO> departmentByMoHuNames = tbDepartmentService.findAllTbDepartmentByMoHuName(pageNum, pageSize, tbDepartment.getName());
        return RespUtil.ok().setData(departmentByMoHuNames);
    }


    /**
     * 导出部门表数据excel
     * @param response
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "导出部门execl", operation = "导出部门execl")
    @PostMapping("/excel")
    @RequiresPermissions("department:export")
    public void excel(HttpServletResponse response, HttpServletRequest request){
        List<TbDepartment> tbDepartmentList = tbDepartmentService.queryAllDepartment();
        List<List<String>> excelData = new ArrayList<>();
        List<String> headList = new ArrayList<>();
        headList.add("编号");
        headList.add("部门名称");
        headList.add("联系电话");
        headList.add("部门地址");
        headList.add("创建时间");
        headList.add("修改时间");
        excelData.add(headList);
        for (TbDepartment tbDepartment: tbDepartmentList) {
            List<String> list = new ArrayList<String>();
            long id = tbDepartment.getId();
            int tempId = (int)id;
            list.add(Integer.toString(tempId));
            list.add(tbDepartment.getName());
            list.add(tbDepartment.getPhone());
            list.add(tbDepartment.getAddress());
            list.add(tbDepartment.getCreateTime().toString());
            list.add(tbDepartment.getModifiedTime().toString());
            excelData.add(list);
        }
        String sheetName = "部门列表";
        String fileName = "部门信息统计表";
        try {
            ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15);
        } catch (IOException e) {
//            logger.info("导出失败");
            System.out.println("导出失败");
        }
    }

//    /**
//     * 查询部门主任，并除去禁用用户
//     * @return
//     */
//    @GetMapping("/findDeanList")
//    public RespUtil findDeanList(){
//        List<UserDeanVO> deanList = tbDepartmentService.findDeanList();
//        return RespUtil.ok().setData(deanList);
//    }

}
